import type { ConfigEnv, UserConfig } from 'vite';
import { loadEnv } from 'vite';
import { resolve } from 'path';
import { generateModifyVars } from './build/generateModifyVars';
import { createVitePlugins } from './build/vite/plugin';

const CWD = process.cwd();

// 环境变量
// const BASE_ENV_CONFIG = loadEnv('', CWD);
// const DEV_ENV_CONFIG = loadEnv('development', CWD);
// const PROD_ENV_CONFIG = loadEnv('production', CWD);

export default ({ command, mode }: ConfigEnv): UserConfig => {
  const env = loadEnv(mode, CWD);

  const isBuild = command === 'build';

  return {
    base: env.VITE_BASE_URL,
    esbuild: {
      // target: 'es2015',
      pure: env.VITE_DROP_CONSOLE ? ['console.log', 'debugger'] : [],
      // drop: env.VITE_DROP_CONSOLE ? ['console', 'debugger'] : [],
    },
    resolve: {
      alias: [
        {
          find: 'vue-i18n',
          replacement: 'vue-i18n/dist/vue-i18n.cjs.js',
        },
        {
          find: '@',
          replacement: resolve(__dirname, './src'),
        },
        {
          find: '_m',
          replacement: resolve(__dirname, './src/modules'),
        },
      ],
    },
    plugins: createVitePlugins(env, isBuild),
    css: {
      preprocessorOptions: {
        less: {
          javascriptEnabled: true,
          // strictMath: false,
          // noIeCompat: true,
          modifyVars: generateModifyVars(),
        },
        // scss: {
        //   additionalData: `
        //   @use 'sass:math';
        //   @import "src/styles/global.scss";
        //   `,
        // },
      },
    },
    server: {
      host: '0.0.0.0',
      port: 8098,
      proxy: {
        '/proxy/': {
          target: 'http://192.168.131.110:9203/',
          // target: "http://192.168.131.7:40572/",
          changeOrigin: true,
          rewrite: (path) => path,
        },
        '/api': {
          // target: "http://10.0.0.249:18132",
          // target: "http://192.168.131.184:42801", // pingjia
          // target: "http://192.168.131.93:40573",
          // target: 'http://192.168.131.110:10087/api', // 盒子
          // target: 'http://10.0.0.249:18126', //云邦
          // target: 'http://10.0.0.249:28167/api', //云邦
          // target: 'http://192.168.110.166:58082', //lb
          // target: 'http://192.168.2.8:58082', //ly
          target: 'http://192.168.121.4:48082', //线上
          // target: 'http://192.168.131.69:85/api', //69
          // target: 'http://192.168.131.20:40573', // qwc
          // target: 'http://192.168.0.166:40573',
          // target: "http://192.168.131.39:52801",
          changeOrigin: true,
          rewrite: (path) => path.replace(/^\/api/, ''),
        },
        '/media': {
          target: 'http://192.168.131.184:8080',
          changeOrigin: true,
          ws: true,
          rewrite: (path) => path,
        },
        '/emqopenapi': {
          target: 'http://192.168.110.58:8081',
          changeOrigin: true,
          rewrite: (path) => path.replace(/^\/emqopenapi/, ''),
        },
      },
    },
    // 优化预编译，提高速度
    optimizeDeps: {
      include: ['lodash-es', 'ant-design-vue/es/locale/zh_CN', 'ant-design-vue/es/locale/en_US', 'vxe-table/lib/locale/lang/zh-CN', 'vxe-table/lib/locale/lang/en-US'], //预编译中强制引入
      exclude: ['vue-demi'], // 在预构建中强制排除的依赖项
    },
    build: {
      // target: 'esnext',
      // minify: 'terser',
      // terserOptions: {
      //   compress: {
      //     keep_infinity: true,
      //     drop_console: Object.is(env.VITE_DROP_CONSOLE, "true")
      //   }
      // }
    },
  };
};
